package com.sam.designpatterns;

/**
 * Created by samwang on 2018/4/21.
 */
public class Adapter {

    public static void main(String[] args) {
        //当前类为Integer
        int myData = 110;
        IntToStringAdapter adapter = new IntToStringAdapter(myData);
        Phone phone = new Phone();
        phone.setMessage(adapter);
        phone.sendMessage();
    }

    interface Message { //目标接口
        String getMessage();//发消息
    }

    static class Phone {

        Message message;

        public void setMessage(Message message) {
            this.message = message;
        }

        public void sendMessage() {
            System.out.println(message.getMessage());
        }
    }

    //int 转 String 的适配器
    static class IntToStringAdapter implements Message {
        int msg;

        public IntToStringAdapter(int msg) {
            this.msg = msg;
        }

        @Override
        public String getMessage() {
            return String.valueOf(msg);
        }
    }
}
